package com.ruoyi.system.mapper;

import com.ruoyi.system.dto.ContractPaymentScheduleCreateDTO;
import com.ruoyi.system.dto.ContractPaymentScheduleDTO;
import com.ruoyi.system.dto.ContractPaymentScheduleUpdateDTO;
import com.ruoyi.system.entity.ContractPaymentSchedule;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 合同付款周期对象映射器
 * 使用MapStruct进行对象转换
 */
@Mapper
public interface ContractPaymentScheduleMapStruct {

    ContractPaymentScheduleMapStruct INSTANCE = Mappers.getMapper(ContractPaymentScheduleMapStruct.class);

    /**
     * 实体类转DTO
     */
    @Mapping(target = "paymentMethodName", expression = "java(getPaymentMethodName(entity.getPaymentMethod()))")
    @Mapping(target = "paymentStatusName", expression = "java(getPaymentStatusName(entity.getPaymentStatus()))")
    ContractPaymentScheduleDTO toDTO(ContractPaymentSchedule entity);

    /**
     * 实体类列表转DTO列表
     */
    List<ContractPaymentScheduleDTO> toDTOList(List<ContractPaymentSchedule> entities);

    /**
     * 创建DTO转实体类
     */
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "paymentStatus", constant = "0")
    @Mapping(target = "actualPaymentDate", ignore = true)
    @Mapping(target = "actualPaymentAmount", ignore = true)
    @Mapping(target = "isDeleted", constant = "0")
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    ContractPaymentSchedule toDO(ContractPaymentScheduleCreateDTO dto);

    /**
     * 更新DTO转实体类
     */
    @Mapping(target = "contractId", ignore = true)
    @Mapping(target = "isDeleted", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    ContractPaymentSchedule toDO(ContractPaymentScheduleUpdateDTO dto);

    /**
     * 获取付款方式名称
     */
    default String getPaymentMethodName(Integer paymentMethod) {
        if (paymentMethod == null) {
            return "未知";
        }
        switch (paymentMethod) {
            case 1:
                return "现金";
            case 2:
                return "银行转账";
            case 3:
                return "支票";
            case 4:
                return "其他";
            default:
                return "未知";
        }
    }

    /**
     * 获取付款状态名称
     */
    default String getPaymentStatusName(Integer paymentStatus) {
        if (paymentStatus == null) {
            return "未知";
        }
        switch (paymentStatus) {
            case 0:
                return "未付款";
            case 1:
                return "已付款";
            case 2:
                return "部分付款";
            case 3:
                return "逾期";
            default:
                return "未知";
        }
    }
}
